package org.lex.ftp.ui;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class Splash {
	// Progress Bar
	static class Progress {
		// position & size
		static float step = 0;
		static int height = 3;
		static Rectangle2D.Float bounds = null;
		// color
		static Color back = Color.black;
		static Color fore = new Color(255, 142, 0);
		// value
		static int maximum = 4;
		static int value = 0;
	}

	// Status Label
	static class Status {
		static final int vgap = 3;
		static final int hgap = 5;
		// position & size
		static int height = 0;
		static Rectangle2D.Float bounds = null;
		// color
		static Color back = Color.black;
		static Color fore = Color.white;
	}

	// ========================================
	private static SplashScreen splash = null;
	private static Graphics2D g2d = null;
	static {
		splash = SplashScreen.getSplashScreen();
		if (null != splash) {
			g2d = splash.createGraphics();
			Dimension size = splash.getSize();
			Status.height = g2d.getFont().getSize();
			Status.bounds = new Rectangle2D.Float(Status.hgap, size.height - 3 * Status.height + Status.vgap, size.width, Status.height + Status.vgap);
			// initialize progressbar
			Progress.bounds = new Rectangle2D.Float(0, size.height - Progress.height, size.width, Progress.height);
			Progress.step = (float) size.width / Progress.maximum;
			g2d.setColor(Progress.back);
			g2d.fill(Progress.bounds);
		}
	}

	public static void setStatus(String status) {
		if (open()) {
			g2d.setColor(Color.white);
			g2d.fill(Status.bounds);
			g2d.setBackground(Color.white);
			g2d.setColor(Color.black);
			g2d.drawString(status, Status.bounds.x, Status.bounds.y + Status.height);
			step(1);
			splash.update();
		}
	}

	private static Rectangle2D.Float block = new Rectangle2D.Float();

	private static void step(int n) {
		if (open()) {
			g2d.setColor(Progress.fore);
			block.x = Progress.value * Progress.step;
			block.y = Progress.bounds.y;
			block.width = n * Progress.step;
			block.height = Progress.height;
			g2d.fill(block);
			Progress.value += n;
			// splash.update();
		}
	}

	private static boolean open() {
		return null != SplashScreen.getSplashScreen();
	}
}
